<?php
###############   COPYLEFT GPLv3 LICENSE   ###############
##
## JFX Version 0.2.7
## Website Management Software
## www.jfxcms.com
##
## Copyright 2009 GPLv3 - http://www.opensource.org/licenses/gpl-3.0.html
##
## Anthony Gallon
## oi_antz@hotmail.com
##
## Permission is hereby granted to any person having a copy of this software
## to freely use and modify as required so long as the copyright notices
## and branding remain intact.
##
## Full license details available at http://www.jfxcms.com/license
##
###############   COPYLEFT GPLv3 LICENSE   ###############








if(Antz_IntelliForm::submitted('new-package')){

    $conn = (int) post('conn');
    $keyname = post('keyname');
    $cData = $DB->fetchRow("SELECT * FROM {$CONFIG->dbprefix}vms_servers WHERE id = '{$conn}'");

    $error = false;

    if(!$VMC->connect($conn)){
        JFX::addError('Could not connect to server');
        $error = true;
    }

    // check for valid package
    $res = $VMC->request($conn, $cData['url'].'/get-package-details/', array('keyname'=>$keyname));

    $res = unserialize($res);

    if(!is_object($res) || $res->result === 'FAIL'){
        JFX::addError('Invalid keyname - package not available on server');
        $error = true;
    }

    if(!$error){
        
        $keyname2 = $DB->escape($keyname);
        $pData = $DB->fetchRow("SELECT * FROM {$CONFIG->dbprefix}vms_packages WHERE keyname = '{$keyname2}'");
        if(!is_array($pData) || count($pData)==0){
            // create new package
            $dbParams = array(
                'keyname' => $res->pData->keyname,
                'base_path' => $res->pData->base_path,
                'created_tstamp' => time(),
                'owner_id' => $USER->userid,
                'current_version' => '0.0.0'
            );
            $id = $DB->insert($CONFIG->dbprefix.'vms_packages', $dbParams);
            $pData = $DB->fetchRow("SELECT * FROM {$CONFIG->dbprefix}vms_packages WHERE id = '{$id}'");
        }
        $dbParams = array(
            'keyname' => $keyname,
            'connection_id' => $conn,
            'package_id' => $pData['id']
        );
        $DB->insert($CONFIG->dbprefix.'vms_subscriptions', $dbParams);

        JFX::addSuccess('Package created, you may now use the update tab to recieve updates');
        JFX::redirect($this->adminUrl.'/updates');
    }

}

























$connOpts = array();
$conns = $DB->fetchAll("SELECT * FROM {$CONFIG->dbprefix}vms_servers ORDER BY url ASC");
foreach($conns as $k=>$v){
    $connOpts[$v['id']] = $this->getContentLang('conn_title', $v['id']);
}

$formFields = array(
    array(
        'type' => 'select',
        'name' => 'conn',
        'value' => post('conn'),
        'options' => $connOpts,
        'label' => 'Connection'
    ),
    array(
        'type' => 'text',
        'name' => 'keyname',
        'value' => post('keyname'),
        'label' => 'Package Keyname'
    ),
    array(
        'type' => 'submit',
        'name' => 'submitBtn',
        'label' => '&nbsp;',
        'value' => 'Create'
    )
);


echo JFX::makeRapidForm($formFields, 'New package details', 'new-package');









